const baseUrl = "/index.php/index/Connectthethird/";

var isProLoaded = false;
var isCityLoaded = false;
var isDealerLoaded = false;
var isModelLoaded = false;
var isSeriesLoaded = false;

var proData, cityData, modelData, seriesData, selectedProCode, selectedProName, selectedCityCode, selectedCityName, selectedModelCode, selectedModelName, selectedSeriesCode, selectedSeriesName;

//alert(baseUrl + "getProvince");
$.ajax({ //获取省
	url: baseUrl + "getProvince",
	success(res) {
		console.log(res);
		proData = res.data;
		if(!proData) {
			alert("数据加载失败！");
			return;
		}
		isProLoaded = true;

		if(isProLoaded && isCityLoaded && isDealerLoaded) {
			onAreaInfoOk()
		}
	}
});
$.ajax({ //获取市
	url: baseUrl + "getCity",
	success(res) {
		console.log(res);
		cityData = res.data;
		if(!cityData) {
			alert("数据加载失败！");
			return;
		}
		isCityLoaded = true;

		if(isProLoaded && isCityLoaded && isDealerLoaded) {
			onAreaInfoOk()
		}
	}
});
var dealerData, selectedDealerCode, selectedDealerName;
$.ajax({ //获取经销商
	url: baseUrl + "getDealers",
	success(res) {
		console.log(res);
		dealerData = res.data;
		if(!dealerData) {
			alert("数据加载失败！");
			return;
		}
		isDealerLoaded = true;

		if(isProLoaded && isCityLoaded && isDealerLoaded) {
			onAreaInfoOk()
		}
	}
});
//地区信息以及经销商的三个ajax执行完成以后的回调
var modelCity = [{
	text: "选择城市",
	value: 0,
	disabled: true
}]; //城市的初始化数据
var citySel; //城市下拉框的实例
var modelDealer = [{
	text: "选择经销店",
	value: 0,
	disabled: true
}]; //经销商的初始化数据
var dealerSel; //经销商下拉框的实例
function onAreaInfoOk() {
	var modelPro = proData.map(function(cur){
		cur.text = cur.province_name;
		cur.value = cur.province_code;
		return cur;
	});
	modelPro.unshift({
		text: "选择省份",
		value: 0,
		disabled: true
	});
	
	$("#selectPro").css("display","inline-block");
	$("#selectDealer").css("display","inline-block");
	$("#selectCity").css("display","inline-block");
	
	//console.log("整理后的省份数据",modelPro);
	$('#selectPro').change(function() {
		//console.log('选中个：'+$(this).val());
		var selectedPro = $(this).val();
		selectedProCode = selectedPro;
		for(var i = 0; i < modelPro.length; i++) {
			if(modelPro[i].province_code == selectedPro) {
				selectedProName = modelPro[i].province_name;
				console.log("选中省份名字", selectedProName);
				break;
			}
		}
		//更新城市数据筛选
		initCityData(selectedPro);
	}).Select({
		width: "55%",
		maxHeight:150,
		maxWidth: 'auto',
		model: modelPro
	});
	//建立城市下拉
	citySel = $('#selectCity').change(function() {
		console.log('选中个：' + $(this).val());
		selectedCityCode = $(this).val();
		for(var i = 0; i < modelCity.length; i++) {
			if(modelCity[i].city_code == selectedCityCode) {
				selectedCityName = modelCity[i].city_name;
				console.log("选中城市名字", selectedCityName);
				break;
			}
		}
		//更新经销商数据
		initDealerData(selectedCityCode);
	}).Select({
		width: "55%",
		maxHeight:130,
		maxWidth: 'auto',
		model: modelCity
	})[0];
	//建立经销商下拉
	dealerSel = $('#selectDealer').change(function() {
		console.log('选中个：' + $(this).val());
		selectedDealerCode = $(this).val();
		for(var i = 0; i < modelDealer.length; i++) {
			if(modelDealer[i].dealer_code == selectedDealerCode) {
				selectedDealerName = modelDealer[i].dealer_name;
				console.log("选中经销商名字", selectedDealerName);
				break;
			}
		}
	}).Select({
		width: "55%",
		maxHeight:90,
		maxWidth: 'auto',
		model: modelDealer
	})[0];
}
//更新城市数据
function initCityData(selectedPro) {
	$('#selectCity').val(0); //初始化城市当前值
	modelCity = [{
		text: "选择城市",
		value: 0,
		disabled: true
	}]; //初始化一下，必须的
	var temArr = cityData.filter(function(cur){
		return cur.city_code.toString().indexOf(selectedPro.toString()) != -1;
	}).map(function(cur){
		cur.text = cur.city_name;
		cur.value = cur.city_code;
		return cur;
	});
	modelCity = modelCity.concat(temArr);
	//console.log("整理后的城市数据",modelCity);
	citySel.setData(modelCity);
};
//更新经销商数据
function initDealerData(selectedCityCode) {
	$('#selectDealer').val(0); //初始化城市当前值
	modelDealer = [{
		text: "选择经销店",
		value: 0,
		disabled: true
	}]; //初始化一下，必须的
	var temArr = dealerData.filter(function(cur){
		return cur.dealer_city_code == selectedCityCode;
	}).map(function(cur){
		cur.text = cur.dealer_name;
		cur.value = cur.dealer_code;
		return cur;
	});
	modelDealer = modelDealer.concat(temArr);
	// console.log("整理后的经销商数据",modelDealer);
	dealerSel.setData(modelDealer);
}

//汽车车型与系列的联动选择-------------------------------
//$.ajax({
//  url:"http://levin.cn/index.php/index/Connectthethird/getCarModel",
//  success(res){
//      console.log(res);
//      modelData = res.data;
//      if(!modelData){
//          alert("数据加载失败！");
//          return;
//      }
//      isModelLoaded = true;
//      if(isModelLoaded&&isSeriesLoaded){
//          onCarInfoOk()
//      }
//  }
//});
//$.ajax({
//  url:"http://levin.cn/index.php/index/Connectthethird/getCarSeries",
//  success(res){
//      console.log(res);
//      seriesData = res.data;
//      if(!seriesData){
//          alert("数据加载失败！");
//          return;
//      }
//      isSeriesLoaded = true;
//      if(isModelLoaded&&isSeriesLoaded){
//          onCarInfoOk()
//      }
//  }
//});
////汽车信息的两个ajax执行完成以后的回调
//var modelSeries = [{text:"选择系列",value:0,disabled:true}];//系列的初始化数据
//var seriesSel;//系列下拉框的实例
//function onCarInfoOk() {
//  var modelModel = modelData.map((cur)=> {
//      cur.text = cur.modle_name;
//      cur.value = cur.modle_code;
//      return cur;
//  });
//  modelModel.unshift({text:"选择车型",value:0,disabled:true});
//  //console.log("整理后的车型数据",modelModel);
//  $('#selectModel').change(function(){
//      // console.log('选中个：'+$(this).val());
//      var selectedModel = $(this).val();
//      selectedModelCode = selectedModel;
//      for(var i=0;i<modelModel.length;i++){
//          if(modelModel[i].modle_code == selectedModel){
//              selectedModelName = modelModel[i].modle_name;
//              // console.log("选中车型名字",selectedModelName);
//              break;
//          }
//      }
//      //每次省份选择以后执行城市数据筛选
//      initSeriesData(selectedModel);
//  }).Select({
//      width:80,
//      maxHeight:300,
//      maxWidth:'auto',
//      model: modelModel
//  });
//  //建立系列下拉
//  seriesSel = $('#selectSeries').change(function(){
//      // console.log('选中个：'+$(this).val());
//      selectedSeriesCode = $(this).val();
//      for(var i=0;i<modelSeries.length;i++){
//          if(modelSeries[i].series_code == selectedSeriesCode){
//              selectedSeriesName = modelSeries[i].series_name;
//              console.log("选中系列名字",selectedSeriesName);
//              break;
//          }
//      }
//  }).Select({
//      width:80,
//      maxHeight:300,
//      maxWidth:'auto',
//      model: modelSeries
//  })[0];
//}
////初始化系列数据
//function initSeriesData(selectedModel){
//  $('#selectSeries').val(0);//初始化城市当前值
//  modelCity = [{text:"选择系列",value:0,disabled:true}];//初始化一下，必须的
//  var temArr = seriesData.filter(cur=>{
//      return cur.modle_code == selectedModel;
//  }).map(cur=>{
//      cur.text = cur.series_name;
//      cur.value = cur.series_code;
//      return cur;
//  });
//  modelSeries = modelSeries.concat(temArr);
//  // console.log("整理后的配置数据",modelSeries);
//  seriesSel.setData(modelSeries);
//}
//提交按钮
var myNotice;
$(".submitBtn").on("click", function() {
	MtaH5.clickStat("liuzi");//统计数据用
	console.log("省", selectedProCode, selectedProName);
	console.log("市", selectedCityCode, selectedCityName);
	console.log("经销商", selectedDealerCode, selectedDealerName);
	console.log("车型", selectedModelCode, selectedModelName);
	console.log("配置", selectedSeriesCode, selectedSeriesName);
	var customer_name = $("#userName").val();
	if(!customer_name) {
		$(document).dialog({
			overlayShow:true,
			titleShow: false,
			overlayClose: true,
			content: '用户名不能为空！',
		});
		return;
	}
	var mobile = $("#mobile").val();
	if(!(/^1[3-8]\d{9}$/.test(mobile))) {
		$(document).dialog({
			overlayShow:true,
			titleShow: false,
			overlayClose: true,
			content: '请输入正确的手机号码！',
		});
		return;
	}
	if(!selectedProCode || !selectedCityCode || !selectedDealerCode) {
		$(document).dialog({
			titleShow: false,
			overlayClose: true,
			content: '请选择经销店！',
		});
		return;
	}
	myNotice = $(document).dialog({
		overlayShow:true,
		type: 'notice',
		infoIcon: 'img/icon/loading.gif',
		infoText: '提交中',
	});
	$.ajax({
		url: baseUrl + "setSalesLeads",
		data: {
			"province_code": selectedProCode,
			"province_name": selectedProName,
			"city_code": selectedCityCode,
			"city_name": selectedCityName,
			"dealer_code": selectedDealerCode,
			"dealer_name": selectedDealerName,
			"modle_code": selectedModelCode,
			"modle_name": selectedModelName,
			"series_code": selectedSeriesCode,
			"series_name": selectedSeriesName,
			"mobile": mobile,
			"customer_name": customer_name,
			"gender": "0"
		},
		success(res) {
			console.log(res);
			if(res.res == 1) {//数据插入成功
				myNotice.update({
					overlayShow:true,
					infoIcon: 'img/icon/success.png',
					infoText: '提交成功！',
					autoClose: 2000
				});
			} else if(res.res == 2){//数据更新成功
				myNotice.update({
					overlayShow:true,
					infoIcon: 'img/icon/success.png',
					infoText: '更新成功！',
					autoClose: 2000
				});
			}else{
				myNotice.update({
					overlayShow:true,
					infoIcon: 'img/icon/fail.png',
					infoText: '提交失败！',
					autoClose: 2000
				});
			}
		},
		fail() {
			myNotice.update({
				overlayShow:true,
				infoIcon: 'img/icon/fail.png',
				infoText: '提交失败！',
				autoClose: 2000
			});
		}
	})
})